// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="OxyPlot">
//   http://oxyplot.codeplex.com, license: Ms-PL
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Diagnostics;
using System.IO;
using Microsoft.Win32;
using OxyPlot.Pdf;

#if SILVERLIGHT
using OxyPlot.Silverlight;
#else
using OxyPlot.Wpf;
#endif

namespace Chart
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class ChartControl : UserControl
    {
        MainViewModel vm = new MainViewModel();

        public void ShowAxis()
        {
            vm.AnotationAdd();
        }

        public void LoadCsv(string name,string name_h,string name_v)
        {
            vm.Open(name,name_h,name_v);
        }

        public ChartControl()
        {
            InitializeComponent();
            DataContext = vm;
        }


        private void Window_Drop(object sender, DragEventArgs e)
        {
#if !SILVERLIGHT
            var data = e.Data as DataObject;
            if (data != null && data.ContainsFileDropList())
            {
                foreach (var file in data.GetFileDropList())
                {
                    vm.Open(file,"Amplitude","Sampling index");
                }
            }
#endif
        }

        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            //Close();
        }

        private  void OpenCsv_Click(object sender, RoutedEventArgs e)
        {
            //OpenCsv();
        }

        public void OpenCsv(string name_h, string name_v)
        {
#if !SILVERLIGHT
            var dlg = new OpenFileDialog();
            dlg.Filter = ".csv files|*.csv";
            dlg.DefaultExt = ".csv";
            if (dlg.ShowDialog().Value)
            {
                vm.Open(dlg.FileName,name_h, name_v);
            }
#endif
        }

        private void SavePlot_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new SaveFileDialog();
            dlg.Filter = ".svg files|*.svg|.png files|*.png|.pdf files|*.pdf|.xaml files|*.xaml";
            dlg.DefaultExt = ".svg";
/*
            if (dlg.ShowDialog(this).Value)
            {
                var ext = Path.GetExtension(dlg.FileName).ToLower();
                switch (ext)
                {
                    case ".png":
                        plot1.SaveBitmap(dlg.FileName);
                        break;
                    case ".svg":
                        vm.Model.SaveSvg(dlg.FileName, plot1.ActualWidth, plot1.ActualHeight);
                        break;
                    case ".pdf":
                        PdfExporter.Export(vm.Model, dlg.FileName, plot1.ActualWidth, plot1.ActualHeight);
                        break;
                    case ".xaml":
                        plot1.SaveXaml(dlg.FileName);
                        break;
                }
                OpenContainingFolder(dlg.FileName);
            }
 */
        }

        private void OpenContainingFolder(string fileName)
        {
#if !SILVERLIGHT
            var psi = new ProcessStartInfo("Explorer.exe", "/select," + fileName);
            Process.Start(psi);
#endif
        }


        private void SaveReport_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new SaveFileDialog();
            dlg.Filter = ".html files|*.html";
            dlg.DefaultExt = ".html";
            //if (dlg.ShowDialog(this).Value)
            //{
            //    vm.SaveReport(dlg.FileName);
            //    OpenContainingFolder(dlg.FileName);
            //}
        }

        private void CopySvg_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(vm.Model.ToSvg(plotWaveform.ActualWidth, plotWaveform.ActualHeight, true));
        }

        private void CopyBitmap_Click(object sender, RoutedEventArgs e)
        {
#if !SILVERLIGHT
            Clipboard.SetImage(plotWaveform.ToBitmap());
#endif
        }

        private void CopyXaml_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(plotWaveform.ToXaml());
        }

        private void HelpAbout_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("by OxyPlot");
        }
    }
}